package cn.lg.soar.system.biz.modules.tenant.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.ThreadContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.core.dto.UserBaseInfo;
import cn.lg.soar.core.util.easyexcel.ExcelUtil;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.tenant.entity.TenantUser;
import cn.lg.soar.system.biz.modules.tenant.service.ITenantUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * TenantUser控制器
 * 租户用户
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "租户用户")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/tenant-user")
public class TenantUserController {

    @Autowired
    private ITenantUserService service;

    private final static String SUBJECT = "租户用户";

    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Valid TenantUser model) {
        Integer tenantId = ThreadContext.getTenantId();
        if (tenantId != 0) {
            model.setTenantId(tenantId);
        }
        AssertUtil.isTrue(service.save(model), "创建失败");
        return HttpResult.success("创建成功");
    }

    @Log(value = "修改" + SUBJECT, type = LogTypeEnum.UPDATE)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Valid TenantUser model) {
        model.setTenantId(null);
        AssertUtil.isTrue(service.updateById(model), "更新失败");
        return HttpResult.success("更新成功");
    }

    @Log(value = "删除" + SUBJECT, type = LogTypeEnum.REMOVE)
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(service.removeByIds(ids), "删除失败");
        return HttpResult.success("删除成功");
    }

    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<TenantUser> detail(@RequestParam(name = "id") @Id Long id) {
        TenantUser data = service.getById(id);
        AssertUtil.notNull(data, "数据不存在");
        return HttpResult.data(data);
    }

    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<TenantUser>> query(PageQuery pageQuery, QueryParams params){
        if (ThreadContext.getTenantId() != 0) {
            params.addFilter(TenantUser::getTenantId, ConditionEnum.eq, ThreadContext.getTenantId());
        }
        PageResult<TenantUser> query = new SqueryWapper<>(TenantUser.class).query(service, pageQuery, params);
        return HttpResult.data(query);
    }

    @Log(value = "导出" + SUBJECT, type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(@RequestParam(name = "filename", required = false) String filename, QueryParams params, HttpServletResponse resp){
        QueryWrapper<TenantUser> wrapper = new SqueryWapper<>(TenantUser.class).listWrapper(params);
        List<TenantUser> list = service.list(wrapper);
        ExcelUtil.exportExcel(filename, list, resp);
    }

    @Log(value = "导入" + SUBJECT, type = LogTypeEnum.IMPORT)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        List<TenantUser> tenants = ExcelUtil.importExcel(req, TenantUser.class);
        AssertUtil.isTrue(service.saveBatch(tenants), "导入失败");
        return HttpResult.success("导入成功");
    }

    @GetMapping("getUser")
    public HttpResult<UserBaseInfo> getUser(@RequestParam(name = "code") String code){
        return HttpResult.data(service.getUser(code));
    }

}

